<template>
	<view class="box_conten">
		<view class="main_bg">
			<scroll-view :refresher-triggered="triggered" refresher-background="transparent"
				@refresherpulling="refresherpulling" refresher-enabled="true" scroll-y="true"
				@scrolltolower="scrolltolower" class="scroll_y" @refresherrefresh="onRefresh">
				<view v-if="goodsList.length>0" class="back_mag">
					<view v-for="(item,index) in goodsList" :key="index" class="item_li">
						<view class="conent" @click="goDetail(item)">
							<view class="left_text">
								<p>商品型号：<span class="conten_text">{{item.model}}</span></p>
								<p>商品规格：<span class="conten_text">{{item.spec}}</span></p>
							</view>
							<view class="right_trl">
								<uni-icons type="right"></uni-icons>
							</view>
						</view>
					</view>
					<view class="no_some" v-if="title">
						{{title}}
					</view>
				</view>
				<view v-else class="void_goods">
					<image class="img" src="/static/goods.svg" mode="aspectFit"></image>
					<view class="tip">暂无数据</view>
				</view>
			</scroll-view>

		</view>
		<uni-popup ref="popupImg" :is-mask-click="false" background-color="#fff" type="center">
			<view
				style="display: flex;flex-direction: column;align-items: center;padding:50rpx 30rpx 30rpx;gap: 20rpx;">
				<view style="display: flex;justify-content: flex-end;width: 100%;position: absolute;top: 0;">
					<uni-icons class="icon" color="#ccc" @click="popupImg.close()" size="32" type="clear"></uni-icons>
				</view>
				<view
					style="border: 1px solid #eee;width: 420rpx;height: 420rpx;padding: 12rpx;margin: 20rpx;z-index: 99;">
					<image mode="aspectFit" :show-menu-by-longpress="true" @click="previewImage"
						style="width: 420rpx;height: 420rpx;z-index: 999;" :src="codeImg">
					</image>
				</view>
				<span style="font-size: 28rpx;color: #007cff;display: flex;align-items: center;justify-content: center;" @click="jumpLink">产品蓝牙遥控小程序 <uni-icons type="right" color="#007cff"></uni-icons></span>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import {
		ref, onMounted
	} from "vue";
	import { onUnload } from '@dcloudio/uni-app'
	import apiCode from '@/util/api/code.js'
	const goodsList = ref([])
	const total = ref(0)
	const page = ref(1)
	const code = ref('')
	const codeImg = ref('')
	const popupImg = ref(null)//弹窗二维码
	const title = ref('没有更多数据了')
	onMounted(() => {
		getList()
	})
	// 下拉刷新触发
	const triggered = ref(false)
	const refresherpulling = (e) => {
		if (e.detail.deltaY < 0) return // 防止上滑页面也触发下拉
		triggered.value = true
	}
	const onRefresh = async () => {
		goodsList.value = []
		page.value = 1
		code.value = ''
		await getList()
		// uni.stopPullDownRefresh(); // 停止下拉刷新状态
		setTimeout(() => {
			triggered.value = false
		}, 500)
	}
	const previewImage = (e) => {
		// uni.previewImage({
		// 			// 需要预览的图片链接列表
		// 			urls: [],
		// 			// 为当前显示图片的链接/索引值
		// 			current: e.target.src,
		// 			// 图片指示器样式	
		// 			indicator:'default',
		// 			// 是否可循环预览
		// 			loop:false,
		// 			// 长按图片显示操作菜单，如不填默认为保存相册
		// 			// longPressActions:{
		// 			// 	itemList:[this.l('发送给朋友'),this.l]
		// 			// },
		// 			success: res => {
		// 				console.log('res', res);
		// 			}, 
		// 			fail: err => {
		// 				console.log('err', err);
		// 			}
		// 		});
	}
	// 长青小程序  家电遥控
	const jumpLink = () => {
		uni.navigateToMiniProgram({
			appId: 'wx2d353c114cb08e18',
			path: 'pages/deviceList/deviceList',
		})
	}
	// 滚到底部触发
	const scrolltolower = () => {
		if (total.value > goodsList.value.length) {
			page.value = page.value + 1
			title.value = ''
			getList()
		} else {
			title.value = '没有更多数据了'
		}
	}
	onUnload(() => {
		uni.hideLoading()
	})
	// 获取商品列表
	const getList = async () => {
		let data = {
			"limit": 0,
			"page": page.value
		}
		uni.showLoading({
			title: '加载中...'
		})
		await apiCode.getBluetoothQrImageGoodsList(data).then((res : { code : number; data : any; }) => {
			if (res.code === 20000) {
				if (res.data) {
					goodsList.value = [...goodsList.value, ...res.data.rows]
					total.value = res.data.total
				}
			}
			uni.hideLoading()
		})
	}
	// 商品详情
	const goDetail = (item) => {
		// uni.navigateTo({
		// 	url: '/pages_user/goods/detail?id=' + id
		// })
		codeImg.value = item.bluetoothQrImage
		popupImg.value.open()
	}
</script>
<style lang="scss" scoped>
	.box_conten {
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
		background-color: #f6f7fa;
		display: flex;
		flex-direction: column;

		.main_bg {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow: hidden;
			height: 100%;

			.scroll_y {
				height: 100%;

				.back_mag {
					display: flex;
					justify-content: center;
					flex-direction: column;
					padding-bottom: 200rpx;
					padding: 24rpx;
					gap: 24rpx;
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 60vh;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}

			.no_some {
				font-size: 24rpx;
				color: #ccc;
				text-align: center;
				width: 100%;
			}

			.item_li {
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				font-size: 28rpx;

				// box-shadow: 0 0 20rpx 10rpx #f0f0f0;
				.conent {
					display: flex;
					align-items: center;
					flex: 1;
					width: 100%;
					background-color: #fff;
					padding: 24rpx;
					box-sizing: border-box;
					justify-content: space-between;

					.left_text {
						display: flex;
						flex-direction: column;
						gap: 6rpx;
						color: #777;

						.conten_text {
							color: #000;
						}
					}

					.right_text {
						font-size: 24rpx;
						margin-right: 12rpx;
						// flex: 1;

						.brands {
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							overflow: hidden;
							word-wrap: break-word;
							word-break: break-all;
							font-weight: bold;
							font-size: 28rpx;
							margin: 0rpx 0 12rpx;
						}
					}
				}
			}
		}
	}
</style>